package com.koalac.ironman.common.module;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.foot.travel.R;
import com.foot.travel.activity.LoginActivity;
import com.foot.travel.activity.PublishPhotoActivity;
import com.foot.travel.event.NetRefreshEvent;
import com.foot.travel.otto.BusProvider;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.koalac.ironman.common.adapter.CommonAdapter;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.ClientProtocolListBase;
import com.koalac.ironman.net.pojo.request.Logout;
import com.koalac.ironman.net.pojo.response.BaseResponse;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.util.GBLActivityManager;
import com.koalac.ironman.util.MyDoubleClickListener;
import com.koalac.ironman.util.ScreenInfoUtil;
import com.koalac.ironman.widget.LoadingDialog;
import com.koalac.ironman.widget.PullToRefreshView;

public abstract class ModuleActivity extends BaseActivity {
	// 头部导航
	protected View tvNav = null;
	// 导航标题
	private TextView tvNavTitle = null;
	// 返回按钮
	protected ImageButton btnBack = null;

	// 内容布局
	protected View contentLayout = null;
	protected View contentRootLayout = null;

	protected ViewGroup rootContentLayout;

	// UI更新消息CODE
	private final int GET_OBJ = 1 << 0;// 网络加载的obj信息
	private final int LIST_UPDATE_FINISH = 1 << 1;// 网络更新列表完成

	LayoutInflater mInflater;
	protected ViewGroup rootContentView;
	protected SlidingMenu mSlidingMenu;
	// 菜单按钮
	protected ImageButton btnMenu = null;
	protected ImageView mEditTravelPhotoView = null;
	protected TextView mPublishPhotoView = null;
	protected TextView mSaveView = null;
	protected ImageView mEditMyInfoView = null;
	protected ImageView mRefreshView = null;
	protected View mNetworkLayout;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.module_layout);
		mInflater = LayoutInflater.from(ModuleActivity.this);
		rootContentView = this.layout();
		this.initLayout(rootContentView);
		initInjectedView(this);

	}

	/**
	 * @Date 2014-06-05
	 * @author chenxiaohua
	 * @return
	 */
	private ViewGroup layout() {
		contentRootLayout = findViewById(R.id.module_layout_content_root);
		contentLayout = findViewById(R.id.module_layout_content);
		tvNav = findViewById(R.id.module_layout_nav);
		/**
		 * 扩大导航栏各个按钮的点击区域范围
		 */
		tvNavTitle = (TextView) findViewById(R.id.module_layout_nav_title);
		rootContentLayout = (ViewGroup) findViewById(R.id.module_layout_content);
		btnBack = (ImageButton) findViewById(R.id.module_layout_back);
		btnBack.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackButtonClicked(v);
			}
		});
		btnMenu = (ImageButton) findViewById(R.id.module_layout_menu);
		btnMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				/**
				 * 打开或关闭侧边栏
				 */
				mSlidingMenu.toggle();
			}
		});
		mEditTravelPhotoView = (ImageView) findViewById(R.id.edit_travel_photo);

		mPublishPhotoView = (TextView) findViewById(R.id.publish_photo);
		mEditMyInfoView = (ImageView)findViewById(R.id.edit_my_info);
		mRefreshView = (ImageView)findViewById(R.id.refresh);
		mSaveView = (TextView) findViewById(R.id.save);
		mNetworkLayout =  findViewById(R.id.network_layout);
		mNetworkLayout.setOnClickListener(new OnClickListener() {
		    
		    @Override
		    public void onClick(View v) {
			postEvent(new NetRefreshEvent());
		    }
		});
		return rootContentLayout;
	}
	
	protected void seSaveButtonVisibility(boolean visible) {
	    mSaveView.setVisibility(visible ? View.VISIBLE : View.GONE);
	}
	protected void setNetworkVisibility(boolean visible) {
	    mNetworkLayout.setVisibility(visible ? View.VISIBLE : View.GONE);
	    contentLayout.setVisibility(visible ? View.GONE : View.VISIBLE);
	}

	protected View addContentView(int layoutID) {
		View childContentView = mInflater.inflate(layoutID, null);
		rootContentLayout.addView(childContentView);
		return childContentView;
	}

	/**
	 * 设置是否需要添加顶部栏的双击事件(功能:双击顶部栏,列表回到顶部)
	 */
	protected void setQuickReturnAvailable(final ListView listView) {
		tvNav.setOnTouchListener(new MyDoubleClickListener(
				new MyDoubleClickListener.EventCallBack() {

					@Override
					public void onDoubleClick() {
						// 平滑滾動會頂部
						// listView.smoothScrollToPosition(0);
						listView.setSelection(0);
					}
				}));
	}

	/**
	 * 设置返回按钮是否可见
	 * 
	 * @Date 2014-06-05
	 * @author chenxiaohua
	 * @param visible
	 */
	protected void setBackButtonVisibility(boolean visible) {
		btnBack.setVisibility(visible ? View.VISIBLE : View.GONE);
	}

	/**
	 * 设置编辑按钮是否可见
	 */
	protected void setEditTravelPhotoVisibility(boolean visible) {
		mEditTravelPhotoView.setVisibility(visible ? View.VISIBLE : View.GONE);
	}

	/**
	 * 设置发布按钮是否可见
	 */
	protected void setPublishPhotoVisibility(boolean visible) {
		mPublishPhotoView.setVisibility(visible ? View.VISIBLE : View.GONE);
	}

	/**
	 * 设置菜单按钮是否可见
	 * 
	 * @Date 2014-06-05
	 * @author chenxiaohua
	 * @param visible
	 */
	protected void setMenuButtonVisibility(boolean visible) {
		btnMenu.setVisibility(visible ? View.VISIBLE : View.GONE);
		if (visible) {
			initSlidMenu();
		}
	}

	private void initSlidMenu() {
		mSlidingMenu = new SlidingMenu(this);
		mSlidingMenu.setMode(SlidingMenu.LEFT);
		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		mSlidingMenu.setShadowWidthRes(R.dimen.dimen_50);
		/**
		 * SlidingMenu划出时主页面显示的剩余宽度
		 */
		mSlidingMenu.setBehindOffset((int) (ScreenInfoUtil
				.getScreenWidth(ModuleActivity.this) - getResources()
				.getDimension(R.dimen.dimen_200)));
		mSlidingMenu.setFadeDegree(0.35f);
		mSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		mSlidingMenu.setMenu(R.layout.homepage_menu);
	}

	/**
	 * 内容根容器，子类的内容务必放置于rootContentView中
	 * 
	 * @Date 2014-06-05
	 * @author chenxiaohua
	 * @param rootContentView
	 */
	protected abstract void initLayout(ViewGroup rootContentView);

	/**
	 * 返回按钮点击事件处理方法
	 * 
	 * @Date 2014-06-05
	 * @author chenxiaohua
	 * @param v
	 */
	protected void onBackButtonClicked(View v) {
		this.onBackPressed();
	}

	/**
	 * 导航栏右侧对象点击事件处理方法
	 * 
	 * @Date 2014-06-05
	 * @author chenxiaohua
	 * @param v
	 */
	protected void onNavigationRightCustomItemClicked(View v) {

	}

	/**
	 * 设置内容布局的背景颜色
	 * 
	 * @param color
	 */
	public void setContentLayoutBackgroudColor(int color) {
		contentLayout.setBackgroundColor(color);
		contentRootLayout.setBackgroundColor(color);
	}

	/**
	 * 设置导航标题颜色
	 * 
	 * @Date 2014-06-05
	 * @author chenxiaohua
	 * @param title
	 */
	protected void setNavigationTitleColor(int titleColor) {
		tvNavTitle.setTextColor(titleColor);
	}

	/**
	 * 设置导航栏右侧按钮
	 * 
	 * @Date 2014-06-05
	 * @author chenxiaohua
	 * @param view
	 */
	public void setNavigationTitle(String title) {
		tvNavTitle.setText(title);
		tvNavTitle.setVisibility(View.VISIBLE);
	}

	public enum LoadState {
		init(0), refresh(1), more(2);
		private int value;

		private LoadState(int value) {
			this.value = value;
		}

		public int getValue() {
			return this.value;
		}
	}

	protected int page = 0;
	protected final int perPage = 10;
	protected LoadingDialog p = null;

	/**
	 * 加载数据
	 */
	public <T extends BaseResponse> void loadData(final Class<T> clazz,
			final LoadState loadState, final ClientProtocolListBase params,
			final PullToRefreshView container, final CommonAdapter<T> adapter,
			final List<T> list) {
		/**
		 * 无网络时的UI表现
		 */
		if (!MyRequest.checkNetWork(ModuleActivity.this)) {
			Toast.makeText(ModuleActivity.this, "当前无网络,请检查网络后再试!",
					Toast.LENGTH_SHORT).show();
			 setNetworkVisibility(true);
			return;
		}
		if (loadState == LoadState.init) {
			page = 0;
			p = LoadingDialog.createLoadingDialog(this, "", "正在加载..");
			p.show();
		} else if (loadState == LoadState.refresh) {
			page = 0;
		} else if (loadState == LoadState.more) {

		}
		params.setPage(page);
		params.setPerpage(perPage);
		MyRequest.request(this, params, new ResponseCallback() {
			@Override
			public void process(boolean result, Context context, Response data,
					String msg) {
				if (loadState == LoadState.init) {
					p.cancel();
				} else if (loadState == LoadState.refresh) {
					container.onHeaderRefreshComplete();
				} else if (loadState == LoadState.more) {
					container.onFooterRefreshComplete();
				}
				if (result) {
					List<T> list = data.getList(clazz);
					if (list != null && list.size() > 0) {
						if (loadState == LoadState.init) {
							adapter.clear();
						} else if (loadState == LoadState.refresh) {
							adapter.clear();
						} else if (loadState == LoadState.more) {
						}
						page += params.getPerpage();
						adapter.addAll(list);
						adapter.notifyDataSetChanged();
						return;
					} else {
						if (loadState == LoadState.init) {
							list.clear();
							p.cancel();
							showNoDataFoundToast();
						} else if (loadState == LoadState.refresh) {
							list.clear();
							p.cancel();
							container.onHeaderRefreshComplete();
							showNoDataFoundToast();
						} else if (loadState == LoadState.more) {
							p.cancel();
							container.onFooterRefreshComplete();
							toast("没有更多数据");
						}
					}
					   setNetworkVisibility(false);
				}else {
				    setNetworkVisibility(true);
				}
				return;
			}
		});
	}
	
	/**
	 * 提示没有数据
	 */
	protected void showNoDataFoundToast() {
		toast("没有数据");
	}
	/**
	 * 提示没有数据
	 */
	protected void showNoDataFoundToast(String text) {
		toast(text);
	}

}
